import java.io.*;

public class TextFileModifier {
    public static void main(String[] args) {
        String filePath = "chaoxian.txt";
        String content = readFile(filePath);
        if (content != null) {
            content = content.replace("金正恩", "金三胖");
            writeFile(filePath, content);
            System.out.println("替换完成。");
        } else {
            System.out.println("文件读取失败或文件不存在。");
        }
    }

    private static String readFile(String filePath) {
        StringBuilder contentBuilder = new StringBuilder();
        try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
            String currentLine;
            while ((currentLine = reader.readLine()) != null) {
                contentBuilder.append(currentLine).append(System.lineSeparator());
            }
            return contentBuilder.toString();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    private static void writeFile(String filePath, String content) {
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
            writer.write(content);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
